package com.whpost.service.zhdt.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whpost.service.entity.t.TCzy;
import com.whpost.service.entity.t.TXzjg;
import com.whpost.service.entity.t.TreeNode;
import com.whpost.service.entity.tools.ListInfo;
import com.whpost.service.entity.tools.PageInfo;
import com.whpost.service.entity.tools.ResultInfo;
import com.whpost.service.service.BaseServices;
import com.whpost.service.service.t.TApiNonceService;
import com.whpost.service.service.t.TXzjgServices;
import com.whpost.service.tools.ToolBean;
import com.whpost.service.zhdt.dao.ZhdtWdzbMapper;
import com.whpost.service.zhdt.dao.ZhdtWgfzMapper;
import com.whpost.service.zhdt.dao.ZhdtZbflMapper;
import com.whpost.service.zhdt.entity.ZhdtWdzb;
import com.whpost.service.zhdt.entity.ZhdtWdzb;
import com.whpost.service.zhdt.entity.ZhdtWgxx;
import com.whpost.service.zhdt.entity.ZhdtWgxxKhxx;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ZhdtWdzbServices extends BaseServices<ZhdtWdzbMapper, ZhdtWdzb> {

    @Resource
    private TApiNonceService tApiNonceService;
    @Resource
    private TXzjgServices tXzjgServices;
    @Resource
    private ZhdtZbflServices zhdtZbflServices;

    @Override
    public ResultInfo info(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            TCzy tCzy = new TCzy();
            tCzy = gettCzyDao().selectById(loginId);

            Map<String,Object> infoMap = new HashMap<>();

            String id="";
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            String lx="";
            if (jsonObject.containsKey("lx")) {
                lx = jsonObject.getString("lx");
            }

            if ("1".equals(lx)){
                ZhdtWdzb zhdtWdzb = new ZhdtWdzb();
                zhdtWdzb = getBaseMapper().selectById(id);
                infoMap.put("zhdtWdzb",zhdtWdzb);
            }

            List<TreeNode> tXzjgTree = tXzjgServices.getTreeSelect(null);
            infoMap.put("tXzjgTree",tXzjgTree);

            List<TreeNode> zhdtZbfTree = zhdtZbflServices.getTreeSelect(null);
            infoMap.put("zhdtZbfTree",zhdtZbfTree);

            //给出默认值

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",infoMap);
            return backRtn(resultInfo,loginId);
        }
    }

    //标注机构的逻辑
    public ResultInfo info1(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            TCzy tCzy = new TCzy();
            tCzy = gettCzyDao().selectById(loginId);

            Map<String,Object> infoMap = new HashMap<>();

            String id="";
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            String lx="";
            if (jsonObject.containsKey("lx")) {
                lx = jsonObject.getString("lx");
            }

            if ("1".equals(lx)){
                ZhdtWdzb zhdtWdzb = new ZhdtWdzb();
                zhdtWdzb = getBaseMapper().selectById(id);
                infoMap.put("zhdtWdzb",zhdtWdzb);
            }

            List<TreeNode> tXzjgTree = tXzjgServices.getTreeSelect(new QueryWrapper<TXzjg>().eq("jb","3").eq("state","1"));
            infoMap.put("tXzjgTree",tXzjgTree);
            //给出默认值

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",infoMap);
            return backRtn(resultInfo,loginId);
        }
    }

    @Override
    public ResultInfo save(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String lx="",id="",nonce_ms="",nonce_zhi="",zbfl="",xzjg="",bjmc="",jd="",wd="";
            if (jsonObject.containsKey("lx")) {
                lx = jsonObject.getString("lx");
            }
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            if (jsonObject.containsKey("zbfl")) {
                zbfl = jsonObject.getString("zbfl");
            }
            if (jsonObject.containsKey("xzjg")) {
                xzjg = jsonObject.getString("xzjg");
            }
            if (jsonObject.containsKey("bjmc")) {
                bjmc = jsonObject.getString("bjmc");
            }
            if (jsonObject.containsKey("jd")) {
                jd = jsonObject.getString("jd");
            }
            if (jsonObject.containsKey("wd")) {
                wd = jsonObject.getString("wd");
            }
            if (jsonObject.containsKey("nonce_ms")) {
                nonce_ms = jsonObject.getString("nonce_ms");
            }
            if (jsonObject.containsKey("nonce_zhi")) {
                nonce_zhi = jsonObject.getString("nonce_zhi");
            }
            //参数校验
            if (StringUtils.isEmpty(zbfl)){
                return new ResultInfo(500,"请选择标记分类！",null);
            }
            if (StringUtils.isEmpty(xzjg)){
                return new ResultInfo(500,"请选择所属机构！",null);
            }
            if (StringUtils.isEmpty(jd)){
                return new ResultInfo(500,"标记经度丢失！",null);
            }
            if (StringUtils.isEmpty(wd)){
                return new ResultInfo(500,"标记维度丢失！",null);
            }

            //校验nonce
            ResultInfo ResultInfoCheckNonce = new ResultInfo();
            ResultInfoCheckNonce = tApiNonceService.checkNonce(nonce_ms,loginId,nonce_zhi,"新增网点周边！");

            if (ResultInfoCheckNonce.getCode()!=200)
            {
                return ResultInfoCheckNonce;
            }

            TCzy tCzy = gettCzyDao().selectById(loginId);

            ZhdtWdzb zhdtWdzb = new ZhdtWdzb();

            if ("1".equals(lx)){
                zhdtWdzb = getBaseMapper().selectById(id);
            }

            zhdtWdzb.setXzjgId(Long.parseLong(xzjg));
            zhdtWdzb.setZbflId(Long.parseLong(zbfl));
            zhdtWdzb.setName(bjmc);
            zhdtWdzb.setZjm(ToolBean.getPy(bjmc));
            zhdtWdzb.setJd(new BigDecimal(jd));
            zhdtWdzb.setWd(new BigDecimal(wd));

            if ("0".equals(lx)){
                zhdtWdzb.setCzyId(tCzy.getId());
                zhdtWdzb.setCzjgId(tCzy.getXzjgId());
                zhdtWdzb.setCreateDate(ToolBean.getToday());
            }

            ResultInfo resultInfo;
            if ("1".equals(lx)){
                if (!String.valueOf(zhdtWdzb.getCzyId()).equals(loginId) && !"1".equals(loginId)){
                    return new ResultInfo(500,"非自己维护的网格信息不得修改！",null);
                }
                getBaseMapper().updateById(zhdtWdzb);
                resultInfo = new ResultInfo(2, "修改成功！",zhdtWdzb);
            }else{
                getBaseMapper().insert(zhdtWdzb);
                resultInfo = new ResultInfo(1, "新增成功！",zhdtWdzb);
            }
            return backRtn(resultInfo,loginId);
        }
    }

    //手工标志网点的保存
    public ResultInfo save1(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String lx="",id="",nonce_ms="",nonce_zhi="",xzjg="",bjmc="",jd="",wd="";
            if (jsonObject.containsKey("lx")) {
                lx = jsonObject.getString("lx");
            }
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            if (jsonObject.containsKey("xzjg")) {
                xzjg = jsonObject.getString("xzjg");
            }
            if (jsonObject.containsKey("bjmc")) {
                bjmc = jsonObject.getString("bjmc");
            }
            if (jsonObject.containsKey("jd")) {
                jd = jsonObject.getString("jd");
            }
            if (jsonObject.containsKey("wd")) {
                wd = jsonObject.getString("wd");
            }
            if (jsonObject.containsKey("nonce_ms")) {
                nonce_ms = jsonObject.getString("nonce_ms");
            }
            if (jsonObject.containsKey("nonce_zhi")) {
                nonce_zhi = jsonObject.getString("nonce_zhi");
            }
            //参数校验
            if (StringUtils.isEmpty(xzjg)){
                return new ResultInfo(500,"请选择所属区局！",null);
            }
            if (StringUtils.isEmpty(jd)){
                return new ResultInfo(500,"标记经度丢失！",null);
            }
            if (StringUtils.isEmpty(wd)){
                return new ResultInfo(500,"标记维度丢失！",null);
            }

            //这里要校验传过来的是否是区局

            //校验nonce
            ResultInfo ResultInfoCheckNonce = new ResultInfo();
            ResultInfoCheckNonce = tApiNonceService.checkNonce(nonce_ms,loginId,nonce_zhi,"新增手工标记网点！");

            if (ResultInfoCheckNonce.getCode()!=200)
            {
                return ResultInfoCheckNonce;
            }

            TCzy tCzy = gettCzyDao().selectById(loginId);

            TXzjg tXzjg = new TXzjg();
            tXzjg.setName(bjmc);
            tXzjg.setZjm(ToolBean.getPy(bjmc));
            tXzjg.setJb(4L);
            tXzjg.setPid(Long.parseLong(xzjg));
            tXzjg.setWdjd(new BigDecimal(jd));
            tXzjg.setWdwd(new BigDecimal(wd));
            tXzjg.setXzjgLb(0L);
            tXzjg.setSfqu("0");

            ResultInfo resultInfo;
            tXzjgServices.getBaseMapper().insert(tXzjg);

            resultInfo = new ResultInfo(1, "新增成功！",tXzjg);
            return backRtn(resultInfo,loginId);
        }
    }

    @Override
    public ResultInfo query(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            TCzy tCzy = gettCzyDao().getCzyById(loginId);

            String curpage = "",pagesize="",zbfl="",xzjg="";
            if (jsonObject.containsKey("curpage")) {
                curpage = jsonObject.getString("curpage");
            }
            if (jsonObject.containsKey("pagesize")) {
                pagesize = jsonObject.getString("pagesize");
            }
            if (jsonObject.containsKey("zbfl")) {
                zbfl = jsonObject.getString("zbfl");
            }
            if (jsonObject.containsKey("xzjg")) {
                xzjg = jsonObject.getString("xzjg");
            }

            if (StringUtils.isEmpty(curpage)) curpage = "1";
            if (StringUtils.isEmpty(pagesize)) pagesize = "10";

            Page<ZhdtWdzb> page = new Page<>( Integer.parseInt(curpage),  Integer.MAX_VALUE);

            ListInfo<ZhdtWdzb> zhdtWdzbListInfo = new ListInfo<>();
            QueryWrapper<ZhdtWdzb> zhdtWdzbQueryWrapper = new QueryWrapper<ZhdtWdzb>();

            if (!StringUtils.isEmpty(zbfl)){
                zhdtWdzbQueryWrapper.like("zbfl_id",zbfl);
            }

            if (!StringUtils.isEmpty(xzjg)){
                zhdtWdzbQueryWrapper.like("xzjg_id",xzjg);
            }

            zhdtWdzbQueryWrapper.orderByDesc("id");

            IPage<ZhdtWdzb> zhdtWdzbPage = getBaseMapper().selectPage(page,zhdtWdzbQueryWrapper);
            PageInfo pageInfo = new PageInfo();
            pageInfo.setTotal(zhdtWdzbPage.getTotal());
            pageInfo.setCurPage(zhdtWdzbPage.getCurrent());
            pageInfo.setPageSize(zhdtWdzbPage.getSize());
            pageInfo.setPageTotal(zhdtWdzbPage.getPages());

            zhdtWdzbListInfo.setPageInfo(pageInfo);
            zhdtWdzbListInfo.setListInfo(zhdtWdzbPage.getRecords());

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",zhdtWdzbListInfo);
            return backRtn(resultInfo,loginId);
        }
    }
}
